package com.hua.Thread.ThreadPerMessage;

/**
 * @author: _hua
 * @created: 2019/4/8.
 * @updater:
 * @description:
 * Thread-Per-Message模式可以提高程序的响应性，但是并不能保证操作的顺序，而且没有返回值。一般用于简单服务器实现，
 * 当服务器主线程收到客户端的请求之后就创建一个新的线程去处理客户端的请求，而服务器主线程则返回继续接收客户端请求。
 */
public class Main {
    public static void main(String[] args) {
        System.out.println("Main BEGIN");
        Host host = new Host();
        host.request(10, 'A');
        host.request(20, 'B');
        host.request(30, 'C');
        System.out.println("Main END");
    }
}


class Host {
    private final Helper helper = new Helper();

    public void request(final int count, final char c) {
        System.out.println("Request BEGIN");
        new Thread() {
            @Override
            public void run() {
                helper.handle(count, c);
            }
        }.start();
        System.out.println("Request END");
    }
}


class Helper {
    public void handle(int count, char c) {
        System.out.println("Helper BEGIN");
        for (int i = 0; i < count; i++) {
            slowly();
            System.out.print(c);
        }
        System.out.println();
        System.out.println("Helper END");
    }

    private void slowly() {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}